/*
 * Copyright 2012 JooKar BV (info@jookar.nl)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.jookar.android.simpleclock.views;

import java.util.Date;

import nl.jookar.android.simpleclock.IClockView;
import nl.jookar.android.simpleclock.R;
import nl.jookar.android.simpleclock.SimpleClockApp;
import nl.jookar.android.simpleclock.R.anim;
import nl.jookar.android.simpleclock.R.id;
import nl.jookar.android.simpleclock.R.layout;
import nl.jookar.android.simpleclock.R.string;

import android.content.Context;
import android.text.format.DateFormat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * @author Joost A. Bloemsma
 *
 */
public class SmoothClockView extends LinearLayout implements IClockView {
	private static final String DEBUG_TAG = SmoothClockView.class.getName();
	private TextView tv0;
	private TextView tv1;
	private Animation animShow;
	private Animation animHide;
	private boolean tv1Visible = true;
	private String format = "";
	
	/**
	 * Constructs a new SmoothClockView instance
	 * @param context
	 */
	public SmoothClockView(Context context) {
		super(context);
		init();
	}
	
	/**
	 * Constructs a new SmoothClockView instance
	 * @param context
	 * @param attrs
	 */
	public SmoothClockView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}
	
	private void init() {
		create();
		getControls();
		animShow = AnimationUtils.loadAnimation(SimpleClockApp.getAppContext(), R.anim.smoothshow);
		animHide = AnimationUtils.loadAnimation(SimpleClockApp.getAppContext(), R.anim.smoothhide);
	}
	
	private void getControls() {
		tv0 = (TextView)findViewById(R.id.tvSmoothClock0);
		tv1 = (TextView)findViewById(R.id.tvSmoothClock1);
	}
	
	private void create() {
		LayoutInflater layoutInflater = (LayoutInflater)SimpleClockApp.getAppContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		layoutInflater.inflate(R.layout.smoothview, this, true);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void setTime(Date date) {
		String time = (String)DateFormat.format(this.format, date);
		if (tv1Visible) {
			tv0.setText(time);
			tv0.startAnimation(animShow);
			tv0.setVisibility(VISIBLE);
			tv1.startAnimation(animHide);
			tv1.setVisibility(INVISIBLE);
		} else {
			tv1.setText(time);
			tv1.startAnimation(animShow);
			tv1.setVisibility(VISIBLE);
			tv0.startAnimation(animHide);
			tv0.setVisibility(INVISIBLE);
		}
		tv1Visible = !tv1Visible;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void set24H(boolean fmt24) {
		if (fmt24) {
			format = SimpleClockApp.getAppContext().getString(R.string.fmt_24h);
		} else {
			format = SimpleClockApp.getAppContext().getString(R.string.fmt_12h);
		}
	}
}
